package com.ruoyi.limitCoupon.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.limitCoupon.domain.LimitCouponConfig;
import com.ruoyi.limitCoupon.service.ILimitCouponConfigService;
import com.ruoyi.technician.mapper.TechnicianMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 限时卡券Controller
 *
 * @author ruoyi
 * @date 2025-04-10
 */
@RestController
@RequestMapping("/limitCouponConfig/limitCouponConfig")
public class LimitCouponConfigController extends BaseController {
    @Autowired
    private ILimitCouponConfigService limitCouponConfigService;
    @Resource
    private TechnicianMapper technicianMapper;

    /**
     * 查询限时卡券列表
     */
    @PreAuthorize("@ss.hasPermi('limitCouponConfig:limitCouponConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(LimitCouponConfig limitCouponConfig) {
        startPage();
        List<LimitCouponConfig> list = limitCouponConfigService.selectLimitCouponConfigList(limitCouponConfig);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (LimitCouponConfig couponConfig : list) {
                if (couponConfig.getCouponScopeApplication() != 0) {
                    String tName = technicianMapper.selectTName(Long.valueOf(couponConfig.getCouponScopeApplication()));
                    couponConfig.setTechnicianName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出限时卡券列表
     */
    @PreAuthorize("@ss.hasPermi('limitCouponConfig:limitCouponConfig:export')")
    @Log(title = "限时卡券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LimitCouponConfig limitCouponConfig) {
        List<LimitCouponConfig> list = limitCouponConfigService.selectLimitCouponConfigList(limitCouponConfig);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (LimitCouponConfig couponConfig : list) {
                if (couponConfig.getCouponScopeApplication() != 0) {
                    String tName = technicianMapper.selectTName(Long.valueOf(couponConfig.getCouponScopeApplication()));
                    couponConfig.setTechnicianName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
                }
            }
        }
        ExcelUtil<LimitCouponConfig> util = new ExcelUtil<LimitCouponConfig>(LimitCouponConfig.class);
        util.exportExcel(response, list, "限时卡券数据");
    }

    /**
     * 获取限时卡券详细信息
     */
    @PreAuthorize("@ss.hasPermi('limitCouponConfig:limitCouponConfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        LimitCouponConfig limitCouponConfig = limitCouponConfigService.selectLimitCouponConfigById(id);
        if (!CheckEmptyUtil.isEmpty(limitCouponConfig)) {
            if (limitCouponConfig.getCouponScopeApplication() != 0) {
                String tName = technicianMapper.selectTName(Long.valueOf(limitCouponConfig.getCouponScopeApplication()));
                limitCouponConfig.setTechnicianName(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
            }
        }
        return success(limitCouponConfig);
    }

    /**
     * 新增限时卡券
     */
    @PreAuthorize("@ss.hasPermi('limitCouponConfig:limitCouponConfig:add')")
    @Log(title = "限时卡券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LimitCouponConfig limitCouponConfig) {
        return toAjax(limitCouponConfigService.insertLimitCouponConfig(limitCouponConfig));
    }

    /**
     * 修改限时卡券
     */
    @PreAuthorize("@ss.hasPermi('limitCouponConfig:limitCouponConfig:edit')")
    @Log(title = "限时卡券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LimitCouponConfig limitCouponConfig) {
        return toAjax(limitCouponConfigService.updateLimitCouponConfig(limitCouponConfig));
    }

    /**
     * 删除限时卡券
     */
    @PreAuthorize("@ss.hasPermi('limitCouponConfig:limitCouponConfig:remove')")
    @Log(title = "限时卡券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(limitCouponConfigService.deleteLimitCouponConfigByIds(ids));
    }
}
